/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package core;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.swing.JFrame;
import javax.swing.JTable;
import util.JTableUtil;

/**
 *
 * @author Kuchcik
 */
public class Accessories {
    
    static String[] selectedRow;
    static JFrame parent;
    public Accessories(JFrame frame){
     this.parent=frame;
    }
    
    
    public static JTable getAllAccessories(){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("select id as ID, nazwa as Nazwa, przeznaczenie as Przeznaczenie, ilosc as Ilosc from akcesoria_lekarskie");
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static JTable getAccessories(String name, float count_from, float count_to, String destiny){
        try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            String query ="select id as ID, nazwa as Nazwa, przeznaczenie as Przeznaczenie, ilosc as Ilosc from akcesoria_lekarskie where ";
            boolean isfirstWhere=true;
            if(name!=null){
            isfirstWhere=false;
            query+="nazwa like \'%"+name+"%\'";
            }
                        
            if(isfirstWhere==false) query+=" and ";
            
            query+= "ilosc between "+count_from+" and "+count_to;
            
            if(isfirstWhere==false && destiny!=null) query+=" and ";
             
            if(destiny!=null){
            query+="przeznaczenie like \'%"+destiny+"%\'";
            }
            
            
            System.out.println(query);
            stmn.execute(query);
            ResultSet rs = stmn.getResultSet();
            return JTableUtil.createTable(rs);
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Błąd połączenia z bazą danych", true,parent);
        }
        return null;
    }
    
    public static void setSelectedRow(String[] row){
        Accessories.selectedRow=row;
    }
    
    public static  String[] getSelectedRow(){
        return Accessories.selectedRow;
    }
    
    public static boolean deleteAccessory(){
            try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("delete from akcesoria_lekarskie where id="+selectedRow[0]);
            return true;
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Nie można usunąć", true,parent);
            return false;
        }
    }
    
    public static boolean addAccessory(String[] row){
            try {
            Statement stmn = SqlConnection.getInstance().getSqlConnection().createStatement(
                    ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
            stmn.execute("insert into akcesoria_lekarskie values('"+row[0]+"','"+row[1]+"',"+row[2]+")");
            return true;
        } catch (SQLException ex ) {
            ErrorLogger.getInstance().log("Nie można dodać(uzupełnij wszystkie miejsca)", true,parent);
            return false;
        }
    }    
}
